//
//  ZLStatusViewModel.swift
//  ZLWeibo
//
//  Created by zhanglongtao on 16/12/28.
//  Copyright © 2016年 hanju001. All rights reserved.
//

import Foundation

/**
 如果没有父类，如果希望在开发的时候调式，输出调式信息，需要
 1，遵守CustomStringConvertible协议
 2.实现description计算属性
 */
class ZLStatusViewModel: CustomStringConvertible {
    
    var status: ZLStstuses
    
    var vipicon: UIImage?
    var memberIcon: UIImage?
    //转发文字
    var reweetStr: String?
    
    //评论文字
    var commetntStr: String?
    //点赞文字
    var likeStr: String?
  
    //配图视图大小
    var pictureViewSize = CGSize()
    
    //如果是转发的微博  原创微博一定没有图
    var picUrls: [ZLStatusPicture]? {
        
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }
    //被转发微博的文字
    var retweetedAttrText: NSAttributedString?
    //微博正文的属性文本
    var statusAttrText: NSAttributedString?
    
    //行高
    var rowHeight: CGFloat = 0
    func updateRowHeight() {
        
        let margin: CGFloat = 12
        let iconHeight: CGFloat = 34
        let toolbarHeight: CGFloat = 35
        
        var height: CGFloat = 0
        
        let viewSize = CGSize(width: UIScreen.hm_screenWidth() - 2 * margin, height: CGFloat(MAXFLOAT))
        
        //计算顶部位置
        height = 2 * margin + iconHeight + margin
        //正文
        if let text = statusAttrText {
            
            //预期尺寸
            height += text.boundingRect(with: viewSize, options: [.usesLineFragmentOrigin], context: nil).height
            
        }
        //判断是否转发微博
        if status.retweeted_status != nil {
            height += 2 * margin
            
            if let text = retweetedAttrText {
                
                height += text.boundingRect(with: viewSize, options: [.usesLineFragmentOrigin], context: nil).height
            }
        }
        //配图视图
        height += pictureViewSize.height
        
        height += margin
        
        height += toolbarHeight
        //使用属性记录
        rowHeight = height
        
    }
    
    init(model: ZLStstuses) {
        
        //会员等级0-6
        self.status = model
        if (model.user?.mbrank)! > 0 && (model.user?.mbrank)! < 7{
            
            let imageName = "common_icon_membership_level\(model.user?.mbrank ?? 1)"
            memberIcon = UIImage(named: imageName)
        }
        //认证图标
        switch model.user?.verified_type ?? -1 {
        case 0:
            vipicon = UIImage(named: "avatar_vip")
        case 2, 3, 5:
            vipicon = UIImage(named: "avatar_enterprise_vip")
        case 220:
            vipicon = UIImage(named: "avatar_grassroot")
        default:
            break
            
        }
        //设置底部计数字符串
        reweetStr = conutString(count: model.reposts_count, defaultStr: "转发")
        commetntStr = conutString(count: model.comments_count, defaultStr: "评论")
        likeStr = conutString(count: model.attitudes_count, defaultStr: "赞")
        
        //计算配图视图大小 有原创的就计算原创的有转发的就计算转发的
        pictureViewSize = calculatePictureViewSize(cout: picUrls?.count ?? 0)
        
        //设置微博文本字体
        let oringinalFont = UIFont.systemFont(ofSize: 15)
        let retweetFont = UIFont.systemFont(ofSize: 14)
        
        //微博正文属性文本
        statusAttrText = EmotionManager.shared.emotionString(string: model.text ?? "", font: oringinalFont)
        //设置被转发微博的文字
        
        let rText = "@" + (status.retweeted_status?.user?.screen_name ?? "") + ":" + (status.retweeted_status?.text ?? "")
        retweetedAttrText = EmotionManager.shared.emotionString(string: rText, font: retweetFont)
        
        //计算行高
        updateRowHeight()
        

    }
    //使用单张图像更新配图视图的大小  网络缓存的单张图像
    func updateSingleImageSize(image: UIImage) {
        
        var sizeq = image.size
        
        let maxWidth: CGFloat = 200
        let minWidth: CGFloat = 40
        //过宽处理
        if sizeq.width > maxWidth {
            
            sizeq.width = maxWidth
            
            sizeq.height = sizeq.width * image.size.height / image.size.width
        }
        //过窄处理
        if sizeq.width < minWidth{
            sizeq.width = minWidth
            
            sizeq.height = sizeq.width * image.size.height / image.size.width / 4
        }
        
        //过高图片处理  高度减小会自动裁切
        if sizeq.height > 200{
            sizeq.height = 200
        }
        
        //特例：有些图像本身就很窄，很长 minHeight思路同上
        
        
        //重新设置配图视图大小
        pictureViewSize = sizeq
        //更新行高
        updateRowHeight()
    }
    private func calculatePictureViewSize(cout: Int) -> CGSize {
        
        if cout == 0{
            
            return CGSize(width: 100, height: 0)
        }
        
        
        let row = (cout - 1) / 3 + 1
        //根据行数算高度
        let height = ZLStatusPictureItemWidth * CGFloat(row) + ZLStatusPictureInnerMargin * CGFloat(row - 1)
        
        return CGSize(width: ZLStatusPictureViewWidth, height: height)
    }
    private func conutString(count: Int, defaultStr: String) -> String{
        
        if count == 0{
            return defaultStr
        }
        
        if count < 10000{
            
            return count.description
        }
        
        return String(format: "%.02f", Double(count)/10000)
        
        
    }
    var description: String{
        
        return status.description
    }
    
}
